package newHtmlToWord;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;

import java.io.File;
import java.io.FileFilter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.net.URL;
import java.net.URLDecoder;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
 * 描述:
 *
 * @author yanzhengwu
 * @create 2020-12-31 11:09
 */
public class Test1234567 {


        public static void main(String[] args) {

            int rows = 5;
            int cols = 5;

            XWPFDocument document = new XWPFDocument();
            XWPFTable table = document.createTable(rows, cols);

            fillTable(table);

            //mergeCellsVertically(table, 3, 1, 3);
            mergeCellsHorizontal(table,2,1,3);

            try {
                Long date = System.currentTimeMillis();
                String path = "F:/htmlToWord/" + date + "text.docx";
                FileOutputStream out = new FileOutputStream(path);
                document.write(out);
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    private static void setCellWith(XWPFTableCell cell, Integer width) {
        //设置列宽
        CTTcPr cPr1 = cell.getCTTc().addNewTcPr();
        CTTblWidth w = cPr1.addNewTcW();
        w.setW(BigInteger.valueOf(width));
        CTTcPr ctTcPr = cell.getCTTc().getTcPr();

    }
    /**
     * @Description: 跨列合并
     */
    public  static void mergeCellsHorizontal(XWPFTable table, int row, int fromCell, int toCell) {
        for (int cellIndex = fromCell; cellIndex <= toCell; cellIndex++) {
            XWPFTableCell cell = table.getRow(row).getCell(cellIndex);
            if ( cellIndex == fromCell ) {
                // The first merged cell is set with RESTART merge value
                cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
            } else {
                // Cells which join (merge) the first one, are set with CONTINUE
                cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
            }
        }
    }
    private static void fillTable(XWPFTable table) {
            for (int rowIndex = 0; rowIndex < table.getNumberOfRows(); rowIndex++) {
                XWPFTableRow row = table.getRow(rowIndex);

                for (int colIndex = 0; colIndex < row.getTableCells().size(); colIndex++) {
                    XWPFTableCell cell = row.getCell(colIndex);
                    setCellWith(cell,5000);
                    cell.addParagraph().createRun().setText(" cell " + rowIndex + colIndex + " ");
                }
            }
        }

    /**
     *
     * @param table
     * @param col 第几列开始
     * @param fromRow 第几行开始
     * @param toRow 合并到第几行
     */
        private static void mergeCellsVertically(XWPFTable table, int col, int fromRow, int toRow) {

            for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {

                XWPFTableCell cell = table.getRow(rowIndex).getCell(col);

                if ( rowIndex == fromRow ) {
                    // The first merged cell is set with RESTART merge value
                    cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
                } else {
                    // Cells which join (merge) the first one, are set with CONTINUE
                    cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
                }
            }
        }



}
